package cn.lyf.controller;

import cn.lyf.entity.Answer;
import cn.lyf.entity.User;
import cn.lyf.respjson.RespJson;
import cn.lyf.service.AnswerService;
import cn.lyf.util.DateFormat;
import cn.lyf.util.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/answer")
public class AnswerController {
    @Autowired
    AnswerService answerService;


    //评论博客
    //blogid:被评论的博客id
    //content:评论的内容
    @RequestMapping("/answer2Blog/{blogid}")
    @ResponseBody
    public Map<String,Object> answer2Blog(@PathVariable("blogid")String blogid,String content){
        System.out.println("blogid=="+blogid);
        User user = SessionUtils.getUser();//获取登录用户
        if(user==null)return RespJson.pack(401,"未登录不可评论",null);
        Answer answer = new Answer();
        //设置发表评论的人的网名
        answer.setNickname(user.getNickname());
        answer.setContent(content);
        answer.setBlogid(blogid);
        //设置发表评论时间
        answer.setTcreate(DateFormat.dateFormat());
        answer = answerService.answer2Blog(blogid, answer);
        return RespJson.pack(200,"评论成功",answer);
    }



    //回复评论
    //groupid:评论博客的分组评论id(以每个评论博客的评论为一组)
    //answerid:被回复的评论id
    @RequestMapping("/answer2Answer/{groupid}/{answerid}")
    @ResponseBody
    public Map<String,Object> answer2Answer(@PathVariable("groupid") String groupid,@PathVariable("answerid")String answerid, String content){
        System.out.println("answerid==");
        System.out.println(answerid);
        System.out.println("content=="+content  );
        User user = SessionUtils.getUser();//获取登录用户
        if(user==null)return RespJson.pack(401,"未登录不可评论",null);
        Answer group = answerService.findById(groupid);
        Answer answer = new Answer();
        String nickname = "";//被回复的网名
        //看下此评论是评论博客的还是评论别人的评论
        //如果是0就是评论博客的,不是0就是回复别人的评论的
        if(!"0".equals(answerid)){
            Answer answer2= answerService.findById(answerid);//查找被回复的评论对象
            nickname = answer2.getNickname();//获取被回复的用户网名
            answer.setContent2(answer2.getContent());//设置被回复的评论内容
//            answer.setAnswerid(answerid);//设置被回复的评论的id
        }else {
            nickname = group.getNickname();//设置被回复的网名
            answer.setContent2(group.getContent());//设置被回复的评论内容
//            answer.setAnswerid(groupid);//设置被回复的网名
        }
        answer.setNickname(user.getNickname());//设置回复别人的网名
        answer.setNickname2(nickname);//设置回复的网名
        answer.setContent(content);//设置回复的内容
        answer.setTcreate(DateFormat.dateFormat());
        List<Answer> answers = group.getAnswers();
        if(answers==null){
           answers = new ArrayList<>();
        }
        answers.add(0,answer);//往头部插入
        group.setAnswers(answers);
        return RespJson.pack(200,"评论成功",answerService.answer2Answer(group,answer));
    }

    //根据回复的分组id获取评论里的评论集合
    @RequestMapping("/getAnswers/{groupid}")
    @ResponseBody
    public Map<String,Object> getAnswers(@PathVariable("groupid")String groupid){
        System.out.println("groupid=="+groupid);
        System.out.println("answers=="+answerService.findById(groupid).getAnswers());
        return  RespJson.pack(200,"",answerService.findById(groupid).getAnswers());
    }




}
